package com.ws.generation.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ws.common.base.entity.Columns;
import com.ws.common.base.entity.JavaConfig;
import com.ws.generation.entity.GenerationConfig;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeParsingEngineUtil {

    //正则需要转换的符号
    static String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };

    public static String sc(String triggerString, JavaConfig config, List<Columns> columns) {
        //字段名处理
        GenerationUtils.setDefaultColumns(columns);
        triggerString=replaceEngine(triggerString,config);
        triggerString=forReplaceEngine(triggerString,columns);
        return triggerString;
    }


    public static String scAdmin(String entityString, GenerationConfig config, List<Columns> columns) {
        GenerationUtils.setDefaultColumns(columns);
        entityString=replaceEngine(entityString,config);
        entityString=forReplaceEngine(entityString,columns);
        return entityString;
    }

    /*
    *   for 循环解析引擎 占时不支持嵌套for循环
    * */
    private static String forReplaceEngine(String triggerString,  List<Columns> columns) {
        Pattern pattern=Pattern.compile("<for.*?column.*?:.*?columns.*?>([\\s\\S]*?)</ *?for.*?>");
        Matcher matcher=pattern.matcher(triggerString);
        while (matcher.find()){
            String code=matcher.group(1);
            String result="";
            for(Columns column:columns){
                result +=  replaceColumnEngine(code,column);
            }
            triggerString=matcher.replaceFirst(result);
            matcher=pattern.matcher(triggerString);
        }
        return triggerString;
    }

    //替换java属性引擎
    private static String replaceEngine(String triggerString,Object config){
        Pattern pattern=Pattern.compile("\\$\\{[ \\t]*?(\\w*)[ \\t]*?\\}");
        Matcher matcher=pattern.matcher(triggerString);
        while (matcher.find()){
            String configName=matcher.group(1);
            triggerString=matcher.replaceFirst(BeanUtil.getFieldValue(config,configName).toString());
            matcher=pattern.matcher(triggerString);
        }
        return triggerString;
    }

    //替换属性列引擎
    private static String replaceColumnEngine(String triggerString,Columns column){
        Pattern pattern=Pattern.compile("\\$\\{[ \\t]*?column\\.(\\w*)[ \\t]*?\\}");
        Matcher matcher=pattern.matcher(triggerString);
        while (matcher.find()){
            String valueName=matcher.group(1);
            if(valueName.equals("dataType")){
                String dataType=getJavaDataType(BeanUtil.getFieldValue(column,valueName).toString());
                triggerString=matcher.replaceFirst(dataType);
            }else{
                String replace=StrUtil.toString(BeanUtil.getFieldValue(column,valueName));
                if(replace==null || replace.equals("null")){
                    replace="";
                }
                triggerString=matcher.replaceFirst(replace);
            }
            matcher=pattern.matcher(triggerString);
        }
        return triggerString;
    }

    private static String getJavaDataType(String dataType) {
        if(dataType.contains("int")) {
            return "Integer";
        }else if( dataType.contains("date") || dataType.contains("time")) {
            return "Date";
        }else if( dataType.contains("double") || dataType.contains("float") || dataType.contains("decimal") ) {
            return "Double";
        }
        return "String";
    }

}
